package com.luckberry.murder.controller;

import com.luckberry.murder.entity.Script;
import com.luckberry.murder.entity.User;
import com.luckberry.murder.entity.game.Game;
import com.luckberry.murder.entity.good.Order;
import com.luckberry.murder.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private UserService userService;
    @Resource
    private GoodService goodService;
    @Resource
    private OrderService orderService;
    @Resource
    private ScriptService scriptService;
    @Resource
    private GameService gameService;
    @Resource
    private NPCService npcService;

    @RequestMapping(value = "/signup", method = RequestMethod.GET)
    public ModelAndView login(HttpSession session) {
        if (session.getAttribute("user") != null) return new ModelAndView("redirect:/index");
        ModelAndView modelAndView = new ModelAndView("signup");
        modelAndView.getModel().put("perm", "admin");
        return modelAndView;
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public ModelAndView users(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("users");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("users", userService.getUsers());
        return modelAndView;
    }

    @RequestMapping(value = "/users/name", method = RequestMethod.GET)
    public ModelAndView usersByName(HttpSession session, String keyword) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("users");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("users", userService.getUsersByName(keyword));
        return modelAndView;
    }

    @RequestMapping(value = "/users/perm", method = RequestMethod.GET)
    public ModelAndView usersByType(HttpSession session, String keyword) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("users");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("users", userService.getUsersByPerm(keyword));
        modelAndView.getModel().put("perm", keyword);
        return modelAndView;
    }

    @RequestMapping(value = "/modify-user", method = RequestMethod.GET)
    public ModelAndView modifyUser(HttpSession session, int uid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-user");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("modified", userService.getUser(uid));
        return modelAndView;
    }

    @RequestMapping(value = "/add-good", method = RequestMethod.GET)
    public ModelAndView addGood(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("add-good");
        modelAndView.getModel().put("user", user);
        return modelAndView;
    }

    @RequestMapping(value = "/modify-good", method = RequestMethod.GET)
    public ModelAndView modifyGood(HttpSession session, int gid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-good");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("good", goodService.getGood(gid));
        return modelAndView;
    }

    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    public ModelAndView orders(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("orders");

        List<Order> orders = orderService.getOrders();
        List<User> users = new ArrayList<>();
        orders.forEach(order -> users.add(userService.getUser(order.getUid())));

        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("orders", orders);
        modelAndView.getModel().put("users", users);
        return modelAndView;
    }

    @RequestMapping(value = "/modify-order", method = RequestMethod.GET)
    public ModelAndView modifyOrder(HttpSession session, int oid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-order");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("order", orderService.getOrder(oid));
        return modelAndView;
    }

    @RequestMapping(value = "/add-script", method = RequestMethod.GET)
    public ModelAndView addScript(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("add-script");
        modelAndView.getModel().put("user", user);
        return modelAndView;
    }

    @RequestMapping(value = "/modify-script", method = RequestMethod.GET)
    public ModelAndView modifyScript(HttpSession session, int sid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-script");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("script", scriptService.getScript(sid));
        return modelAndView;
    }

    @RequestMapping(value = "/modify-npc", method = RequestMethod.GET)
    public ModelAndView modifyNPC(HttpSession session, int nid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-npc");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("npc", npcService.getNPC(nid));
        return modelAndView;
    }

    @RequestMapping(value = "/add-npc", method = RequestMethod.GET)
    public ModelAndView addNPC(HttpSession session, int sid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("add-npc");
        modelAndView.getModel().put("user", user);
        modelAndView.getModel().put("sid", sid);
        return modelAndView;
    }

    @RequestMapping(value = "/modify-game", method = RequestMethod.GET)
    public ModelAndView modifyGame(HttpSession session, int gid) {
        User user = (User) session.getAttribute("user");
        if (user == null || !user.getPerm().equals("admin")) return new ModelAndView("redirect:/login");
        ModelAndView modelAndView = new ModelAndView("modify-game");
        modelAndView.getModel().put("user", user);
        Game game = gameService.getGame(gid);
        modelAndView.getModel().put("game", game);
        modelAndView.getModel().put("owner", userService.getUser(game.getUid()));
        return modelAndView;
    }
}
